//
//  LayerContentViewController.m
//  CoreAnimationTestProject
//
//  Created by guopengwen on 16/5/17.
//  Copyright © 2016年 guopengwen. All rights reserved.
//

#import "LayerContentViewController.h"

@interface LayerContentViewController ()

@property (weak, nonatomic) IBOutlet UIView *layerView;

@property (weak, nonatomic) IBOutlet UIImageView *img_one;


@property (weak, nonatomic) IBOutlet UIImageView *img_two;
@property (weak, nonatomic) IBOutlet UIImageView *img_three;
@property (weak, nonatomic) IBOutlet UIImageView *img_four;

@property (weak, nonatomic) IBOutlet UIButton *btnOne;


@property (weak, nonatomic) IBOutlet UIButton *btnTwo;


@end

@implementation LayerContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIImage *img = [UIImage imageNamed:@"juxingtu.jpg"];
    
    self.layerView.layer.contents = (__bridge id)img.CGImage;
    // 内容的适应方式
    _layerView.layer.contentsGravity = kCAGravityCenter;
    // kCAGravityCenter; // 此时contentsScale起作用
    //  kCAGravityResizeAspect;
     _layerView.layer.contentsScale = img.scale *10;
    _layerView.layer.masksToBounds = YES;
    _layerView.layer.contentsRect = CGRectMake(0, 0, 1, 1);
    
    [self modifySpriteImagView];
}

- (void)modifySpriteImagView
{
    UIImage *img = [UIImage imageNamed:@"duoxingzhuangtu.png"];
    [self addSpriteImage:img withContentRect:CGRectMake(0, 0, 0.5, 0.5) layer:_img_one.layer];
    [self addSpriteImage:img withContentRect:CGRectMake(0.5, 0, 0.5, 0.5) layer:_img_two.layer];
    [self addSpriteImage:img withContentRect:CGRectMake(0, 0.5, 0.5,0.5) layer:_img_three.layer];
    [self addSpriteImage:img withContentRect:CGRectMake(0.5, 0.5, 0.5, 0.5) layer:_img_four.layer];
    
    UIImage *btnImg = [UIImage imageNamed:@"buttonBackGround_2x1"];
    [self addStretchableImage:btnImg withContentCenter:CGRectMake(0.15, 0.15, 0.7, 0.7) layer:_btnOne.layer];
    [self addStretchableImage:btnImg withContentCenter:CGRectMake(0, 0, 1, 1) layer:_btnTwo.layer];
 
    CALayer *blueLayer = [CALayer layer];
    blueLayer.frame = CGRectMake(10,10, 80, 80);
    blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    blueLayer.delegate = self;
    blueLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layerView.layer addSublayer:blueLayer];
    
    // 手动调用重绘
    [blueLayer display];
}


#pragma mark -- CAlayerDelegate
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGContextSetLineWidth(ctx, 5);
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextStrokeEllipseInRect(ctx, layer.bounds);
}





// 设置layer的适应
- (void)addSpriteImage:(UIImage *)img withContentRect:(CGRect)rect layer:(CALayer *)layer
{
    layer.contents = (__bridge id)img.CGImage;
    layer.contentsGravity = kCAGravityResizeAspect;
    layer.contentsRect = rect;
}

// 设置layer的拉伸
- (void)addStretchableImage:(UIImage *)img withContentCenter:(CGRect)rect layer:(CALayer *)layer
{
    layer.contents = (__bridge id)img.CGImage;
    layer.contentsCenter = rect;
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
